<template>
  <div class="settings">
    <van-nav-bar safe-area-inset-top placeholder :title="$t('AdvancedSetting')" left-arrow fixed @click-left="onClickLeft" />
    <van-pull-refresh v-model="isLoading" :pulling-text="$t('PullingText')" :loosing-text="$t('PullingText')" :success-text="$t('refreshSuccee')" @refresh="onRefresh">
      <van-collapse class="marginTop15" v-model="activeNames">
        <van-collapse-item title="ModbusRTU/485" name="1">
          <pfLockInPointPTVue :setName="$t('ModBus485Address')" :setMin="1" :setMax="255" :setValue.sync="LocalArr.ModBus485AddressValue" :reg="37"></pfLockInPointPTVue>
          <languagePTVue :setName="$t('ModBus485BraudRate')" :columns="ModBus485Arr" :setValue="LocalArr.ModBus485BraudRateValue" :reg="38"></languagePTVue>
          <languagePTVue :setName="$t('ModbusControlPriority')" :columns="toggleArr" :setValue="LocalArr.ModbusControlPriorityValue" :reg="60"></languagePTVue>
          <languagePTVue :setName="$t('ModbusPriorityTimeout')" :columns="ModbusTimeoutArr" :setValue="LocalArr.ModbusPriorityTimeoutValue" :reg="61"></languagePTVue>
          <languagePTVue :setName="$t('LocalRemoteSetup')" :columns="LocalRemoteSetupArr" :setValue="LocalArr.LocalRemoteSetupValue" :reg="62"></languagePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('Grid')" name="2">
          <languagePT2Vue :setName="$t('VacLower')" :setUntil="'V'" :columns="VacMinArr" :setValue.sync="LocalArr.VacMinValue" :reg="5" :pre="10"></languagePT2Vue>
          <languagePT2Vue :setName="$t('TimeVacMinAdj')" :setUntil="'ms'" :columns="TimeVacMinAdjArr" :setValue.sync="LocalArr.TimeVacMinAdjValue" :reg="9" :pre="1"></languagePT2Vue>
          <languagePT2Vue :setName="$t('VacUpper')" :setUntil="'V'" :columns="VacMaxArr" :setValue.sync="LocalArr.VacMaxValue" :reg="6" :pre="10"></languagePT2Vue>
          <languagePT2Vue :setName="$t('TimeVacMaxAdj')" :setUntil="'ms'" :columns="TimeVacMinAdjArr" :setValue.sync="LocalArr.TimeVacMaxAdjValue" :reg="10" :pre="1"></languagePT2Vue>
          <languagePT2Vue :setName="$t('FacLower')" :setUntil="'Hz'" :columns="facMins50Arr" :setValue.sync="LocalArr.FacMinValue" :reg="7" :pre="100" v-if="isHz50"></languagePT2Vue>
          <languagePT2Vue :setName="$t('FacLower')" :setUntil="'Hz'" :columns="facMins60Arr" :setValue.sync="LocalArr.FacMinValue" :reg="7" :pre="100" v-if="isHz60"></languagePT2Vue>
          <languagePT2Vue :setName="$t('TimeFacMinAdj')" :setUntil="'ms'" :columns="TimeVacMinAdjArr" :setValue.sync="LocalArr.TimeFacMinAdjValue" :reg="11" :pre="1"></languagePT2Vue>
          <languagePT2Vue :setName="$t('FacUpper')" :setUntil="'Hz'" :columns="facMaxs50Arr" :setValue.sync="LocalArr.FacMaxValue" :reg="8" :pre="100" v-if="isHz50"></languagePT2Vue>
          <languagePT2Vue :setName="$t('FacUpper')" :setUntil="'Hz'" :columns="facMaxs60Arr" :setValue.sync="LocalArr.FacMaxValue" :reg="8" :pre="100" v-if="isHz60"></languagePT2Vue>
          <languagePT2Vue :setName="$t('TimeFacMaxAdj')" :setUntil="'ms'" :columns="TimeVacMinAdjArr" :setValue.sync="LocalArr.TimeFacMaxAdjValue" :reg="12" :pre="1"></languagePT2Vue>
          <languagePT2Vue :setName="$t('PhaseJumpDegree')" :setUntil="'±°'" :columns="PhaseJumpArr" :setValue.sync="LocalArr.phaseJumpValue" :reg="48" :pre="1"></languagePT2Vue>
          <languagePT2Vue :setName="$t('TimeStart')" :setUntil="'S'" :columns="timeStartArr" :setValue.sync="LocalArr.timeStartValue" :reg="2" :pre="1"></languagePT2Vue>
        </van-collapse-item>
        <van-collapse-item :title="$t('ExportControl')" name="3">
          <pfLockInPointPTVue :setMin="0" :setMax="60000" :setName="$t('ExportControl')" :setUntil="'W'" :setValue.sync="LocalArr.ExportLimitValue" :reg="40"></pfLockInPointPTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('MaximumChargingPower')" name="4">
          <pfLockInPointPTVue :setName="$t('GreenModeMaxAcPower')" :setUntil="'W'" :setMin="0" :setMax="ratedpower" :setValue.sync="LocalArr.GreenModeMaxAcPowerValue" :reg="25"></pfLockInPointPTVue>
          <pfLockInPointPTVue :setName="$t('EconomicModeMaxAcPower')" :setUntil="'W'" :setMin="0" :setMax="ratedpower" :setValue.sync="LocalArr.EconomicModeMaxAcPowerValue" :reg="26"></pfLockInPointPTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('EPS')" name="5">
          <languagePT2Vue :setName="$t('EpsLowSoc')" :setUntil="'%'" :columns="EpsArr" :setValue.sync="LocalArr.EpsLowSocValue" :reg="20" :pre="1"></languagePT2Vue>
          <languagePT2Vue :setName="$t('EpsRestartSoc')" :setUntil="'%'" :columns="EpsArr" :setValue.sync="LocalArr.EpsRestartSocValue" :reg="21" :pre="1"></languagePT2Vue>
          <languagePTVue :setName="$t('EpsFreqSet')" :columns="OffGridOperatingFrequencyArr" :setValue="LocalArr.OffGridOperatingFrequencyValue" :reg="59"></languagePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('Reset')" name="6">
          <resetINVEnergyPTVue :setName="$t('ClearInverterHistory')" :reg="45"></resetINVEnergyPTVue>
          <resetINVEnergyPTVue :setName="$t('ResetErrorLogs')" :reg="44"></resetINVEnergyPTVue>
          <resetINVEnergyPTVue :setName="$t('ClearNotice')" :reg="43"></resetINVEnergyPTVue>
          <resetINVEnergyPTVue :setName="$t('Restorefactorysettings')" :reg="46"></resetINVEnergyPTVue>
          <resetINVEnergyPTVue :setName="$t('ClearEpsOverloadFault')" :reg="42"></resetINVEnergyPTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('ReactiveMode')" name="7">
          <van-form @submit="powerfactorSubmit">
            <van-field :value="LocalArr.factMode" :label="$t('ModeSelect')" readonly clickable name="picker" input-align="right" @click="showPowerFact = true" />
            <van-popup v-model="showPowerFact" round position="bottom">
              <van-picker show-toolbar :columns="factorArr" 
                @cancel="showPowerFact = false" 
                @confirm="factModeConfirm"
                :confirm-button-text="$t('OK')"
                :cancel-button-text="$t('Cancel')"  
              />
            </van-popup>
            <van-field v-if="factModeCode==1||factModeCode==2" 
              v-model="LocalArr.P2Value" 
              :label="$t('PFValue')" 
              type="number" 
              show-error 
              name="validator" 
              input-align="right" 
              error-message-align="right" 
              clearable 
              :rules="[{ validator:fixedTwo1,message: `${$t('Range')}：0.80-1.00` }]" 
            />
            <van-field v-if="factModeCode==5&&MachineType==0"
              v-model="LocalArr.bQpower_setValue" 
              :label="$t('bQpower_set')" 
              type="text" 
              show-error 
              name="validator" 
              input-align="right" 
              error-message-align="right" 
              clearable 
              :rules="[{ validator:fixedTwo2,message: `${$t('Range')}：-2000- 2000` }]" 
            />
            <van-field v-if="factModeCode==5&&MachineType==1"
              v-model="LocalArr.bQpower_setValue" 
              :label="$t('bQpower_set')" 
              type="text" 
              show-error 
              name="validator" 
              input-align="right" 
              error-message-align="right" 
              clearable 
              :rules="[{ validator:fixedTwo3,message: `${$t('Range')}：-4000- 4000` }]" 
            />
            <div class="factorBtnBox">
              <van-button block round>{{$t('Save')}}</van-button>
            </div>
          </van-form>
        </van-collapse-item>
        <van-collapse-item :title="$t('SafetyFunctionSwitch')" name="8">
          <languagePTVue :setName="$t('FRTFunction')" :columns="toggleArr" :setValue="LocalArr.FRTFunctionValue" :reg="52"></languagePTVue>
          <languagePTVue :setName="$t('ActIslanding')" :columns="toggleArr" :setValue="LocalArr.ActIslandingValue" :reg="53"></languagePTVue>
          <languagePTVue :setName="$t('PassIslanding')" :columns="toggleArr" :setValue="LocalArr.PassIslandingValue" :reg="54"></languagePTVue>
          <languagePTVue :setName="$t('CtCheck')" :columns="toggleArr" :setValue="LocalArr.CtCheckValue" :reg="56"></languagePTVue>
          <languagePTVue :setName="$t('MotivationState')" :columns="MotivationArr" :setValue="LocalArr.MotivationStateValue" :reg="57"></languagePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('MpptScanMode')" name="9">
          <languagePTVue :columns="MpptScanArr" :setValue="LocalArr.MpptScanModeValue" :reg="58"></languagePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('SystemVoltageRiseSuppression')" name="10">
          <languagePTVue :setName="$t('VriseSuppression')" :columns="toggleArr" :setValue="LocalArr.VriseSuppressionValue" :reg="55"></languagePTVue>
          <vacMaxPTVue :setName="$t('PuStarVolt')" :setUntil="'V'" :setMin="105" :setMax="112.5" :setValue.sync="LocalArr.PuStarVoltValue" :reg="63"></vacMaxPTVue>
          <vacMaxPTVue :setName="$t('PuDecVolt')" :setUntil="'V'" :setMin="107" :setMax="114.5" :setValue.sync="LocalArr.PuDecVoltValue" :reg="64"></vacMaxPTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('NewPassword')" name="11">
          <newPasswordPTVue :setValue.sync="LocalArr.NewPasswordValue" :reg="47"></newPasswordPTVue>
        </van-collapse-item>
      </van-collapse>
    </van-pull-refresh>
  </div>
</template>

<script>
import pfLockInPointPTVue from '../../../components/PfLockInPoint/PfLockInPointPT'
import vacMaxPTVue from '../../../components/VacMax/VacMaxPT'
import facMaxPTVue from '../../../components/FacMax/FacMaxPT'
import freDroopDelayTimePTVue from '../../../components/FreDroopDelayTime/FreDroopDelayTimePT'
import languagePTVue from '../../../components/Language/LanguagePT'
import resetINVEnergyPTVue from '../../../components/ResetINVEnergy/ResetINVEnergyPT'
import newPasswordPTVue from '../../../components/NewPassword/NewPasswordPT'
import languagePT2Vue from '../../../components/Language/Language2PT'

export default {
  components:{
    pfLockInPointPTVue,
    vacMaxPTVue,
    facMaxPTVue,
    freDroopDelayTimePTVue,
    languagePTVue,
    resetINVEnergyPTVue,
    newPasswordPTVue,
    languagePT2Vue
  },
  data(){
    return {
      isLoading:false,
      timer:'',
      activeNames:[],
      ModBus485Arr:[115200,57600,56000,38400,19200,14400,9600],
      VacMinArr:[80,82.5,85,87.5,90],
      TimeVacMinAdjArr:[500,1000,1500,2000],
      VacMaxArr:[110,112.5,115,117.5,120],
      isHz50:false,
      isHz60:false,
      facMins50Arr:[47.5,48,48.5,49,49.5],
      facMins60Arr:[57,57.6,58.2,58.8,59.4],
      facMaxs50Arr:[50.5,51,51.5],
      facMaxs60Arr:[60.6,61.2,61.8],
      PhaseJumpArr:[3,5,7,10],
      timeStartArr:[1,5,150,300],
      EpsArr:[10,20,30,40,50,60,70,80,90,100],
      showPowerFact:false,
      factorArr:[{text:this.$t('Off'),code:0},{text:this.$t('OverExcited1'),code:1},{text:this.$t('UnderExcited1'),code:2},{text:this.$t('Q-Set'),code:5}],
      factModeCode:'',
      MachineType:0,
      toggleArr:[this.$t('Disable'),this.$t('Enable')],
      MotivationArr:[this.$t('Standby'),this.$t('Usually')],
      MpptScanArr:[this.$t('Close'),this.$t('low'),this.$t('Medium'),this.$t('High')],
      ModbusTimeoutArr:['10min','30min',this.$t('Close'),'5min'],
      LocalRemoteSetupArr:[this.$t('Grounded'),this.$t('Remotely')],
      OffGridOperatingFrequencyArr:['50Hz','60Hz'],
      ratedpower:'',
      LocalArr:{
        ModBus485AddressValue:'',
        ModBus485BraudRateValue:'',
        ModbusControlPriorityValue:'',
        ModbusPriorityTimeoutValue:'',
        LocalRemoteSetupValue:'',
        VacMinValue:'',
        TimeVacMinAdjValue:'',
        VacMaxValue:'',
        TimeVacMaxAdjValue:'',
        FacMinValue:'',
        TimeFacMinAdjValue:'',
        FacMaxValue:'',
        TimeFacMaxAdjValue:'',
        phaseJumpValue:'',
        timeStartValue:'',
        ExportLimitValue:'',
        GreenModeMaxAcPowerValue:'',
        EconomicModeMaxAcPowerValue:'',
        EpsLowSocValue:'',
        EpsRestartSocValue:'',
        OffGridOperatingFrequencyValue:'',
        factMode:'',
        P2Value:'',
        bQpower_setValue:'',
        FRTFunctionValue:'',
        ActIslandingValue:'',
        PassIslandingValue:'',
        VriseSuppressionValue:'',
        CtCheckValue:'',
        MotivationStateValue:'',
        MpptScanModeValue:'',
        PuStarVoltValue:'',
        PuDecVoltValue:'',
        NewPasswordValue:''
      }
    }
  },
  methods: {
    onClickLeft(){
      this.$router.push({path:'/setting'});
    },
    factModeConfirm(value){
      this.showPowerFact=false;
      this.LocalArr.factMode=value.text;
      this.factModeCode=value.code;
    },
    fixedTwo1(val){
      return this.comm.fixedTwo(val,0.8,1);
    },
    fixedTwo2(val){
      let valid;
      if(val>=-2000&&val<=2000){
        valid=true;
      }else{
        valid=false;
      }
      return valid;
    },
    fixedTwo3(val){
      let valid;
      if(val>=-4000&&val<=4000){
        valid=true;
      }else{
        valid=false;
      }
      return valid;
    },
    async getData(){
      this.comm.loadingShow();
      let datas=await this.SETAPI.setGetDataPT(localStorage.getItem('domain'),{'tokenId':localStorage.getItem('tokenId'),'sn':localStorage.getItem('wifiSN')});
      if(datas.result!=null&&datas.result!="tokenId不合法!"){
        let dataArr = datas.result;
        this.LocalArr.ModBus485AddressValue=dataArr[47];
        this.LocalArr.ModBus485BraudRateValue=dataArr[48];
        this.LocalArr.ModbusControlPriorityValue=dataArr[67];
        this.LocalArr.ModbusPriorityTimeoutValue=dataArr[68];
        this.LocalArr.LocalRemoteSetupValue=dataArr[69];
        this.LocalArr.VacMinValue=dataArr[4]/10;
        this.LocalArr.TimeVacMinAdjValue=dataArr[8];
        this.LocalArr.VacMaxValue=dataArr[5]/10;
        this.LocalArr.TimeVacMaxAdjValue=dataArr[9];
        //判断范围
        if(dataArr[53]==0){
          this.isHz50=true;
          this.isHz60=false;
        }else{
          this.isHz50=false;
          this.isHz60=true;
        }
        this.LocalArr.FacMinValue=dataArr[6]/100;
        this.LocalArr.TimeFacMinAdjValue=dataArr[10];
        this.LocalArr.FacMaxValue=dataArr[7]/100;
        this.LocalArr.TimeFacMaxAdjValue=dataArr[11];
        this.LocalArr.phaseJumpValue=dataArr[54];
        this.LocalArr.timeStartValue=dataArr[1];
        this.LocalArr.ExportLimitValue=dataArr[50];
        this.LocalArr.GreenModeMaxAcPowerValue=dataArr[35];
        this.LocalArr.EconomicModeMaxAcPowerValue=dataArr[36];
        this.LocalArr.EpsLowSocValue=dataArr[31];
        this.LocalArr.EpsRestartSocValue=dataArr[32];
        this.LocalArr.OffGridOperatingFrequencyValue=dataArr[66];
        this.factModeCode=dataArr[55];
        this.factorArr.forEach(elem=>{
          if(elem.code==this.factModeCode){
            this.LocalArr.factMode=elem.text;
          }
        });
        this.LocalArr.P2Value=dataArr[56]/100;
        this.LocalArr.bQpower_setValue=this.comm.bit16(0,dataArr[57]);
        //判断范围
        if(dataArr[51]==0){
          this.MachineType=0;//判断机型
          this.ratedpower=3000;//获取最大充电功率最大取值范围
        }else{
          this.MachineType=1;
          this.ratedpower=6000;
        }
        this.LocalArr.FRTFunctionValue=dataArr[59];
        this.LocalArr.ActIslandingValue=dataArr[60];
        this.LocalArr.PassIslandingValue=dataArr[61];
        this.LocalArr.VriseSuppressionValue=dataArr[62];
        this.LocalArr.CtCheckValue=dataArr[63];
        this.LocalArr.MotivationStateValue=dataArr[64];
        this.LocalArr.MpptScanModeValue=dataArr[65];
        this.LocalArr.PuStarVoltValue=dataArr[70]/10;
        this.LocalArr.PuDecVoltValue=dataArr[71]/10;
        this.LocalArr.NewPasswordValue=this.comm.showPwd(dataArr[58]);
      }else{
        this.comm.tipsShow('InvNoResponse');
      }
    },
    //设置powerfactor
    powerfactorSubmit(){
      //处理数据
      let factorData_new={};
      factorData_new.P1Value=this.factModeCode;
      if(this.LocalArr.P2Value!=null&&this.LocalArr.P2Value!=undefined){
        factorData_new.P2Value=this.comm.mulFunction(this.LocalArr.P2Value,100);
      }
      factorData_new.bQpower_setValue=this.LocalArr.bQpower_setValue;
      let params=[
        {'reg':49,'val':factorData_new.P1Value},
        {'reg':50,'val':factorData_new.P2Value},
        {'reg':51,'val':factorData_new.bQpower_setValue}
      ];
      this.comm.setDataPT(3,params);
    },
    onRefresh(){
      this.isLoading = false;
      this.timer=new Date().getTime();
      for(let i in this.LocalArr){
        this.LocalArr[i]='';
      }
      this.getData();
    }
  },
  mounted(){
    this.getData();
  }
}
</script>

<style scoped lang="less">

</style>
